package com.molyfun.parents.activity.ask.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.GetMediaCommentsResponse;
import com.molyfun.parents.rest.bean.SendCommentResponse;
import com.molyfun.parents.rest.bean.TopicDetailBean;
import com.molyfun.parents.utils.EmptyHelper;
import com.molyfun.parents.utils.UIUtils;
import com.molyfun.parents.view.RecordView.ChatInputMenu;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class SendCommentBarView extends LinearLayout implements View.OnClickListener, ChatInputMenu.ChatInputMenuListener{

    public static final int COMMENT_TYPE_REPLY = 1;//回复－和老师之间上面部分的内容
    public static final int COMMENT_TYPE_COMMENT = 2;//评论区域

    //状态根据当前话题是否是热门问题而定
    public int mCurrentCommentType = COMMENT_TYPE_COMMENT;

    protected LayoutInflater layoutInflater;

    private Context context;

    private String mTopicDetailId;
    private String mTargetCommentid;

    @InjectView(R.id.input_menu)
    ChatInputMenu mInputMenu;

    private int mCurrentOperationType = TopicOperationView.OperationType.TYPE_TOPIC;

//    @InjectView(R.id.comment_input)
//    MfEditText mCommentInput;
//    @InjectView(R.id.send)
//    TextView mSend;

    public SendCommentBarView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public SendCommentBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SendCommentBarView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_send_comment_bar, this);
        ButterKnife.inject(this);
        mInputMenu.init();
        mInputMenu.setChatInputMenuListener(this);

        mInputMenu.setEditTextHint("@请填写回复");

    }

    @Override
    public void onSendMessage(String content) {
        if(!TextUtils.isEmpty(content)){
            sendComment(content);
        }
    }

    public ChatInputMenu getInputMenu(){
        return mInputMenu;
    }

    //            @NeedsPermission(Manifest.permission.RECORD_AUDIO)
    @Override
    public boolean onPressToSpeakBtnTouch(View v, MotionEvent event) {
        if(mSendCommentCallback != null){
            return mSendCommentCallback.onPressToSpeakBtnTouch(v, event);
        }
        return true;
    }

    public void initData(TopicDetailBean topicDetailBean) {
        initData(topicDetailBean, TopicOperationView.OperationType.TYPE_TOPIC);
    }

    /**
     * init data
     * and associated to TopicDetailBean
     */
    public void initData(TopicDetailBean topicDetailBean, int type) {
        mCurrentOperationType = type;
        mTopicDetailId = topicDetailBean.getId();
        if(EmptyHelper.isTopicDetailPublic(topicDetailBean)){
            mCurrentCommentType = COMMENT_TYPE_COMMENT;
        }else{
            mCurrentCommentType = COMMENT_TYPE_REPLY;
        }
    }

    public void associateToReplyBean(GetMediaCommentsResponse.MediaCommentsReply replyBean){
        if(replyBean != null){
            mTargetCommentid = replyBean.getId();
            mInputMenu.setEditTextHint("@回复" + replyBean.getUsername());
        }
    }

    public void removeAssociateToReplyBean(){
        mTargetCommentid = null;
        mInputMenu.setEditTextHint("@请填写回复");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            default:

                break;
        }
    }

    private void sendComment(String content){
        if(mCurrentOperationType == TopicOperationView.OperationType.TYPE_TOPIC){
            Call<SendCommentResponse> call = MFPApplication.getInstance().getRestClient().sendComment(content, mCurrentCommentType, mTopicDetailId, mTargetCommentid, 4, null, null, 0);
            call.enqueue(new Callback<SendCommentResponse>() {
                @Override
                public void onResponse(Call<SendCommentResponse> call, Response<SendCommentResponse> response) {
                    if (response.body().getCode() == 200) {
                        UIUtils.showToast(getContext(), "发送评论成功");
                        if(mSendCommentCallback != null){
                            mSendCommentCallback.onSendCommentSuccess(response.body().getData());
                        }
                    }else{
                        UIUtils.showToast(getContext(), response.body().getMsg());
                        if(mSendCommentCallback != null){
                            mSendCommentCallback.onSendCommentFailed();
                        }
                    }
                }

                @Override
                public void onFailure(Call<SendCommentResponse> call, Throwable t) {
                    UIUtils.showToast(getContext(), "发送评论失败");
                    if(mSendCommentCallback != null){
                        mSendCommentCallback.onSendCommentFailed();
                    }
                }
            });
        }else{
            Call<SendCommentResponse> call = MFPApplication.getInstance().getRestClient().sendHomelandComment(content, mTopicDetailId, mTargetCommentid, 4, null, null, 0);
            call.enqueue(new Callback<SendCommentResponse>() {
                @Override
                public void onResponse(Call<SendCommentResponse> call, Response<SendCommentResponse> response) {
                    if (response.body().getCode() == 200) {
                        UIUtils.showToast(getContext(), "发送评论成功");
                        if(mSendCommentCallback != null){
                            mSendCommentCallback.onSendCommentSuccess(response.body().getData());
                        }
                    }else{
                        UIUtils.showToast(getContext(), response.body().getMsg());
                        if(mSendCommentCallback != null){
                            mSendCommentCallback.onSendCommentFailed();
                        }
                    }
                }

                @Override
                public void onFailure(Call<SendCommentResponse> call, Throwable t) {
                    UIUtils.showToast(getContext(), "发送评论失败");
                    if(mSendCommentCallback != null){
                        mSendCommentCallback.onSendCommentFailed();
                    }
                }
            });
        }
    }

    private SendCommentCallback mSendCommentCallback;

    public void setSendCommentCallback(SendCommentCallback sendCommentCallback){
        this.mSendCommentCallback = sendCommentCallback;
    }

    public static interface SendCommentCallback{
        public void onSendCommentSuccess(GetMediaCommentsResponse.MediaCommentsReply replyBean);
        public void onSendCommentFailed();
        public boolean onPressToSpeakBtnTouch(View v, MotionEvent event);
    }

}